/**
 * @file
 * A JavaScript file for the theme.
 *
 * In order for this JavaScript to be loaded on pages, see the instructions in
 * the README.txt next to this file.
 */

// JavaScript should be made compatible with libraries other than jQuery by
// wrapping it with an "anonymous closure". See:
// - https://drupal.org/node/1446420
// - http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
(function ($, Drupal, window, document, undefined) {


// To understand behaviors, see https://drupal.org/node/756722#behaviors
Drupal.behaviors.datepicker = {
  attach: function(context, settings) {
    if($(".date-picker").length > 0)
      $(".date-picker").datepicker();    
  }
};


})(jQuery, Drupal, this, this.document);
var in_progress = false;
jQuery(document).ready(function() {
    (function($, Drupal) {
        var base_path = Drupal.settings.basePath + Drupal.settings.pathPrefix;
        $('.icon_close').click(function() {
            var that = this;
           $('.promo-outer').animate({height: 0}, 500, 'linear', function() {
               $(that).remove();
           });
        });
        
        $('.new_message_modal').click(function() {
            var username = $(this).attr('rel');
            if($('#myModal').size()) {
                $('#myModal').remove();
            }
            var url = base_path + 'new_message';
            if(in_progress) {
                in_progress.abort();
            }
            in_progress = $.ajax({
                url: url,
                success: function(sendMessageBoxHtml) {
                    sendMessageBoxHtml = sendMessageBoxHtml.replace('{username}', username);
                    $('body').append(sendMessageBoxHtml);
                    $('#myModal').modal({
                        show: true
                    });

                    $('button.clear').click(function() {
                        $("#input_message").val("");
                    });

                    $('button.btn-save').click(function() {
                        var $content = $('#edit-body-value');
                        if($.trim($('#edit-body-value').val()) == '') {
                            $content.focus();
                            return;
                        }
                        if(in_progress) {
                            in_progress.abort();
                        }
                        in_progress = $.ajax({
                            url: url,
                            type: 'POST',
                            data: $('#privatemsg-new').serialize(),
                            success: function(txt) {
                                $("#myModal .modal-body").html(txt);
                                $('button.btn-save').remove();
                                $('button.clear').remove();
                            }
                        });
                    });
                }
            });
        });
        $('.commerce-order-cancel a').click(function() {
            if(confirm(Drupal.t('Are you sure to cancel?'))) {
                return true;
            }
            return false;
        });
        $('.commerce-order-confirm a').click(function() {
            if(confirm(Drupal.t('Are you sure to confirm?'))) {
                return true;
            }
            return false;
        });
        
       $(".commerce-order-user_complete_confirm a").click(function() {
           if(confirm(Drupal.t('Are you sure to pay to guide?'))) {
                return true;
            }
            return false;
       });
    })(jQuery, Drupal);
});
